package com.bootdo.cash.controller;

import com.bootdo.cash.service.CashService;
import com.bootdo.cash.service.ExamineService;
import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.order.domain.RechargeDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Desc 提现审核
 * @date 2019/12/24
 *
 */
@Controller
@RequestMapping("/cash/examine")
public class ExamineController {

    private final String PREFIX = "cash/examine";
    @Autowired
    private ExamineService examineService;
    @Autowired
    private CashService cashService;


    @RequiresPermissions("cash:examine:examine")
    @Log("提现审核")
    @GetMapping()
    String examine(){
        return PREFIX + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("cash:examine:examine")
    PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<RechargeDO> list = examineService.list(query);
        int total = examineService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @RequiresPermissions("cash:examine:examine")
    @Log("获取发起人备注")
    @PostMapping("/findUser")
    @ResponseBody
    R findUser(Long userId) {
        String name = cashService.findUser(userId);
        return R.ok(name);
    }

    @Log("查看凭证")
    @GetMapping("/showImg/{id}")
    String showImg(Model model, @PathVariable("id") Long id){
        RechargeDO recharge = examineService.get(id);
        model.addAttribute("recharge", recharge);
        return PREFIX + "/showImg";
    }

    @RequiresPermissions("cash:examine:edit")
    @Log("修改审核状态")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id){
        RechargeDO recharge = examineService.get(id);
        model.addAttribute("recharge", recharge);
        return PREFIX + "/edit";
    }

    @RequiresPermissions("cash:examine:edit")
    @Log("保存审核状态")
    @PostMapping("/update")
    @ResponseBody
    @IpFilter
    R update(RechargeDO recharge){
        int result = examineService.update(recharge);
        if (result > 0) {
            return R.ok();
        } else if (result == -2) {
            return R.error("当前提现已审核，请勿重复审核");
        }
        return R.error();
    }
}
